/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package movierental.ui.servlet;

/**
 *
 * @author abet
 */

import java.io.IOException;
import java.math.BigDecimal;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import movierental.entity.Movie;
import movierental.service.AppService;

@SuppressWarnings("serial")
public class EditMovie extends HttpServlet {
	
	private AppService service;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		ApplicationContext ctx = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext());
		
		service = (AppService) ctx.getBean("appService");
	}



	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		resp.setContentType("text/html");
		          System.out.println(req.getRequestURI());
		String output = "<html>";
		output += "<head>";
		output += "<title>Daftar Movie</title>";
		output += "</head>";
		output += "<body>";
		output += "<h1>Edit Movie</h1>";

		output += "<form method='post' action='saveMovie'>";
		output += "<table>";
		output += "<tr>";
		output += "<td>Kode</td>";
		output += "<td><input type='text' name='kodeMovie'></td>";
		output += "</tr>";
		
		output += "<tr>";
		output += "<td>Judul</td>";
		output += "<td><input type='text' name='judulMovie'></td>";
		output += "</tr>";
		
                output += "<tr>";
		output += "<td>Format</td>";
		output += "<td><input type='text' name='formatMovie'></td>";
		output += "</tr>";
		
                output += "<tr>";
		output += "<td>Harga</td>";
		output += "<td><input type='text' name='hargaMovie'></td>";
		output += "</tr>";
		
                
		output += "<tr>";
		output += "<td>&nbsp;</td>";
		output += "<td><input type='submit' value='Simpan'></td>";
		output += "</tr>";
		
		
		output += "</table>";
		output += "</form>";
		
		output += "</body>";
		output += "</html>";
		
		resp.getWriter().print(output);
		resp.getWriter().close();
		
	}



	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String kode = req.getParameter("kodeMovie");
		String judul = req.getParameter("judulMovie");
                String format = req.getParameter("formatMovie");
                BigDecimal harga = new BigDecimal(req.getParameter("hargaMovie"));
		
                
                
		Movie m = new Movie();
		m.setKodeMovie(kode);
                m.setJudulMovie(judul);
                m.setFormatMovie(format);
                m.setHargaMovie(harga);
		
		service.saveMovie(m);
		resp.sendRedirect("list.asp");
	}
	
	
	
}